package com.ws.util;
/*
自定义数组的工具类
 */
public class ArrayUtil {

    //求数组的最大值
    public static int getMax(int[] arr) {
        int maxNum = 0;
        for (int ar: arr) {
            maxNum = maxNum > ar? maxNum: ar;
        }
        return maxNum;
    }

    //求数组的最小值
    public static int getMin(int[] arr) {
        int minNum = 0;
        for (int ar: arr) {
            minNum = minNum < ar? minNum: ar;
        }
        return minNum;
    }

    //求数组的总和
    public static int getSum(int[] arr) {
        int sum = 0;
        for (int ar: arr) {
            sum += ar;
        }
        return sum;
    }

    //求数组的平均值
    public static int gatAvg(int[] arr) {
        int avg = 0;
        for (int ar: arr) {
            avg += ar;
        }
        avg = avg / arr.length;
        return avg;
    }

    //反转数组
    public static void reverse(int[] arr) {
        for (int i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    //复制数组
    public static int[] copy(int[] arr) {
        return null;
    }

    //数组排序
    public static void sort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    //遍历数组
    public static void print(int[] arr) {

    }

    //查找指定元素
    public static int getIndex(int[] arr, int key) {
        for (int i = 0; i < arr.length; i++) {
            if (key == arr[i]) {
                return i;
            }
        }
        return - 1;
    }
}
